package microcredit.gui.handler;

import java.awt.Component;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

import microcredit.gui.editor.AbstractEditorForm;
import microcredit.gui.editor.EditorPanel;
import microcredit.gui.editor.group.GroupGeneralForm;
import microcredit.gui.editor.group.GroupMemberForm;
import microcredit.gui.selector.IFilter;
import microcredit.model.Member;
import microcredit.model.MemberGroup;
import microcredit.model.bo.BOMember;
import microcredit.model.bo.BOMemberGroup;
import microcredit.model.domain.Status;

public class GroupHandler extends AbstractHandler<MemberGroup>{

	@Override
	public MemberGroup save(MemberGroup entity) throws Throwable {
		return BOMemberGroup.create().saveMemberGroup(entity);
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<IFilter<MemberGroup>> getFilters() {
		return Arrays.asList(new MemberGroupNameFilter(), new MemberGroupStatusFilter());
	}
	
	@Override
	public MemberGroup refresh(MemberGroup element) throws Throwable {
		if (element.getId() == null) {
			return element;
		}
		return BOMember.create().getMemberGroup(element.getId());
	}
	
	@Override
	public void delete(MemberGroup e) throws Throwable {
		BOMemberGroup.create().deleteMemberGroup(e);
	}
	
	@Override
	public MemberGroup newEntity(Component parent) throws Throwable {
		List<AbstractEditorForm<MemberGroup>> forms = new ArrayList<AbstractEditorForm<MemberGroup>>();
		forms.add(new GroupGeneralForm());
		forms.add(new GroupMemberForm());
		MemberGroup e = new MemberGroup();
		e = EditorPanel.showEditorDialog(parent, "Edit", 
				MemberGroup.class, e, this, this, null, forms);
		return e;
	}
	
	@Override
	public MemberGroup open(Component component, MemberGroup e) throws Throwable {
		List<AbstractEditorForm<MemberGroup>> forms = new ArrayList<AbstractEditorForm<MemberGroup>>();
		forms.add(new GroupGeneralForm());
		forms.add(new GroupMemberForm());
		e = EditorPanel.showEditorDialog(component, "Edit", 
				MemberGroup.class, e, this, this, null, forms);
		return e;
	}
	
	@Override
	public List<MemberGroup> load() throws Throwable {
		return BOMemberGroup.create().readMemberGroups();
	}
	
	protected final class MemberGroupStatusFilter implements IFilter<MemberGroup> {
		@Override
		public String getLabel() {
			return "Status";
		}

		@Override
		public List<? extends Object> getValues() {
			return Arrays.asList(Status.values());
		}
		
		@Override
		public Object getSelectedValue() {
			return null;
		}

		@Override
		public void filter(Object oexp, List<MemberGroup> entities) {
			Status exp = (Status)oexp;
				
			for (Iterator<MemberGroup> iterator = entities.iterator(); iterator
					.hasNext();) {
				MemberGroup member = iterator.next();
				if (member.getStatus() != exp) {
					iterator.remove();
				}
			}
		}
	}
	
	protected final class MemberGroupNameFilter implements IFilter<MemberGroup> {
		@Override
		public String getLabel() {
			return "Name";
		}

		@Override
		public List<Object> getValues() {
			return null;
		}
		
		@Override
		public Object getSelectedValue() {
			return null;
		}

		@Override
		public void filter(Object oexp, List<MemberGroup> entities) {
			String exp = ((String)oexp).toLowerCase();
			String[] expressions = exp.split("\\ ");
			for (Iterator<MemberGroup> iterator = entities.iterator(); iterator.hasNext();) {
				MemberGroup memberGroup = iterator.next();
				String fullName = memberGroup.getFullName().toLowerCase();
				for (String subExp : expressions) {
					if (fullName.indexOf(subExp) == -1) {
						iterator.remove();
						break;
					}
				} 
			}
		}
	}
	
}
